package com.rem.designPattern.prototype原型.deepclone1;

/**
 * 深拷贝：1.复制对象的所有基本数据类型的成员变量值
 * *      2.为所有引用数据类型的成员变量申请存储空间，并复制每个引用数据类型成员变量所引用的对象，直到该对象可达的所有对象
 * 深拷贝-重写 clone 方法来实现深拷贝
 *
 * @author Rem
 * @date 2022-11-15
 */
public class Sheep implements Cloneable {

    private String name;
    private int age;

    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Sheep clone() throws CloneNotSupportedException {
        return (Sheep) super.clone();
    }
}

class Shepherd implements Cloneable {

    private String name;

    private Sheep sheep;

    public Shepherd(String name, Sheep sheep) {
        this.name = name;
        this.sheep = sheep;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Sheep getStudent() {
        return sheep;
    }

    public void setStudent(Sheep sheep) {
        this.sheep = sheep;
    }

    @Override
    protected Shepherd clone() throws CloneNotSupportedException {
        //基本数据类型拷贝
        Shepherd shepherd = (Shepherd) super.clone();
        //引用类型拷贝
        shepherd.sheep = sheep.clone();
        return shepherd;
    }
}